/**
 * AssessmentController.java
 * Version 1.0.0
 * Created on 2018年8月27日
 *
 */
package com.yincheng.oes.business.assessment.controller;

import com.yincheng.oes.business.assessment.service.AssessmentService;
import com.yincheng.oes.business.config.PaperResultStatus;
import com.yincheng.oes.business.exam.base.entity.Paper;
import com.yincheng.oes.business.exam.judgepaper.entity.PaperResult;
import com.yincheng.oes.business.exam.judgepaper.entity.PaperStudentResult;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import static com.yincheng.oes.business.util.PaperUtils.getAnswersList;

/**
 * @author yangsy
 * @version 1.0.0
 * @since
 * 创建时间：2018年8月27日
 * <p>
 * 功能描述：对外评估服务controller
 * <p>
 * 邮箱地址：446948434@qq.com
 */
@Controller
@RequestMapping("/external/assessment")
public class AssessmentController {

    @Autowired
    private AssessmentService assessmentService;

    /**
     * 评估答卷页面
     *
     * @param view
     * @param paperId
     * @return
     */
    @Deprecated
    @RequestMapping(value = "/draftPage.html", method = RequestMethod.GET)
    public ModelAndView draftPage(ModelAndView view, String paperId) {
        setView(view);

        PaperStudentResult form = assessmentService.getNewPaperByPaperId(paperId);
        view.addObject("form", form);
        view.setViewName("/business/assessment/paper-draft");
        return view;
    }

    @RequestMapping(value = "/answerPage.html", method = RequestMethod.GET)
    public ModelAndView answerPage(ModelAndView view, String paperId, String userName) {
        setView(view);

        PaperStudentResult form = assessmentService.getNewPaperByPaperId(paperId);
        form.setUserName(userName);
        view.addObject("form", form);
        view.setViewName("/business/assessment/paper-draft");
        return view;
    }

    @RequestMapping(value = "/draftPrePage.html", method = RequestMethod.GET)
    public ModelAndView draftPrePage(ModelAndView view, String paperId) {
        Paper paper = assessmentService.getBasePaperByPaperId(paperId);
        view.addObject("form", paper);
        view.addObject("paperId", paperId);
        view.setViewName("/business/assessment/paper-pre");
        return view;
    }

    @RequestMapping(value = "/startAnswer", method = RequestMethod.POST)
    public ModelAndView startAnswer(ModelAndView view, String paperId, String userName) {
        PaperStudentResult paperResult = assessmentService.getPaperResult(paperId, userName);
        String resultId = paperResult.getId();
        view.addObject("resultId", resultId);
        view.setViewName("redirect:/external/assessment/openPaperPage.html");
        return view;
    }

    @RequestMapping(value = "/openPaperPage.html", method = RequestMethod.GET)
    public ModelAndView openPaperPage(ModelAndView view, String resultId) {
        setView(view);
        PaperStudentResult paperResult = assessmentService.getPaperResult(resultId);
        view.addObject("form", paperResult);
        view.setViewName("/business/assessment/paper-draft");
        if ("3".equals(paperResult.getStatus())) {
            view.setViewName("/business/assessment/paper-read");
        }
        return view;
    }

    private void setView(ModelAndView view) {
        List<String> answers = getAnswersList();
        view.addObject("answers", answers);
    }

    @RequestMapping(value = "/submitPaperResult.json", method = RequestMethod.POST)
    @ResponseBody
    public PaperStudentResult submitPaperResult(PaperResult bean) {
        bean.setStatus(PaperResultStatus.END_ANSWER.getStatus());// 回答完毕
        PaperStudentResult result = assessmentService.savePaperResult(bean);
        return result;
    }

    @RequestMapping(value = "/readPage.html", method = RequestMethod.GET)
    public ModelAndView readPage(ModelAndView view, String resultId) {
        PaperStudentResult paperResult = assessmentService.getPaperResult(resultId);
        view.addObject("form", paperResult);
        setView(view);

        view.setViewName("/business/assessment/paper-read");
        return view;
    }

    @InitBinder // 类初始化是调用的方法注解
    public void initBinder(WebDataBinder binder) {
        // 给这个controller配置接收list的长度1024，仅在这个controller有效
        binder.setAutoGrowCollectionLimit(1024);
    }
}
